package kr.moiba.result.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.moiba.vertification.service.VertificationSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller
@RequestMapping("/result")
public class ResultCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private VertificationSvc vertificationSvc;
		
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		String userId = (String)session.getAttribute("userId");
		condition.put("userId", userId);
		
		resultMap.put("result", vertificationSvc.findbyListResult(condition));
		return resultMap;
	}
	
	@RequestMapping(value={"/listDetail.do"})
	public Map<String, Object> listDetail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="type") String type) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		String userId = (String)session.getAttribute("userId");
		condition.put("viewer", userId);
		if(type.equals("pay")){
			condition.put("type", 2);	
		}else{
			condition.put("type", 1);
		}

		//페이징 처리 parameter		
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		resultMap.putAll(vertificationSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}
	
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="idx") Integer idx) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
				
		resultMap.put("vertification", vertificationSvc.findby(idx));
		
		//history
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("vIdx", idx);
		resultMap.put("vertificationHistory", vertificationSvc.findbyListHistory(condition));
		
		return resultMap;
	}

}